package 数据结构.栈_队列;

import java.util.Stack;

/**
 * @author Zhu
 * @Description
 * @create 2023-02-24
 */
public class JZ73翻转单词序列 {
    public static String ReverseSentence(String str) {
        if(str == "" || str.length() == 0)
            return "";
        int length = str.length();
        StringBuilder sb = new StringBuilder();
        StringBuilder sb1 = new StringBuilder();
        for(int i=length-1;i>=0;i--){
            if(str.charAt(i)!=' '){
                sb1.append(str.charAt(i));
            }else{
                sb.append(sb1.reverse()).append(" ");
                sb1.delete(0,sb1.length());
            }
            if(i==0){
                sb.append(sb1.reverse()).append(" ");
                sb1.delete(0,sb1.length());
            }
        }
        sb.deleteCharAt(sb.length()-1);
        return sb.toString();
    }

    //栈
    public static String ReverseSentence2(String str) {
        if(str == "" || str.length()==0)
            return "";

        String[] s = str.split(" ");
        Stack<String> ss = new Stack<>();
        for(int i=0;i<s.length;i++){
            ss.push(s[i]);
            ss.push(" ");
        }
        StringBuilder sb = new StringBuilder();
        while (!ss.isEmpty()){
            sb.append(ss.pop());
        }
        sb.deleteCharAt(0);
        return sb.toString();

    }



    public static void main(String[] args) {
        String s = "     i am a hello     ";
        String[] s1 = s.split(" ");
        for (String ss : s1){
            System.out.println(ss);
        }
    }


}
